package a0.xml;

import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * author: xiaoxu
 **/
public class XMLDemo {

    @Test
    public void saxParseXML() throws Exception {
        //1.创建一个sax解析器工厂对象
        SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
        //2.通过工厂对象创建sax解析器
        SAXParser saxParser = saxParserFactory.newSAXParser();
        //3.创建一个数据处理器
        StudentHandler studentHander = new StudentHandler();
        //4.开始解析
        InputStream inputStream = Thread.currentThread()
                .getContextClassLoader()
                .getResourceAsStream("a0\\xml\\student.xml");
        saxParser.parse(inputStream, studentHander);

        List<Student> students = studentHander.getStudents();
        for (Student student : students) {
            System.out.println(student.toString());
        }

    }


    /*
        1.基于树形结构，
        2.消耗内存：通过解析器一次记载到内存当中
        3.能够随机读取，更加灵活，适合在web应用中使用
     */
    @Test
    public void domParseXml() throws ParserConfigurationException, IOException, SAXException {
        //1.创建dom解析工厂
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        //2.通过工厂创建解析器对象
        DocumentBuilder documentBuilder = factory.newDocumentBuilder();
        //3.解析文档
        InputStream inputStream = Thread.currentThread()
                .getContextClassLoader()
                .getResourceAsStream("a0\\xml\\student.xml");
        //此行代码完成后，xml文档已经被加载到内存中，以树状结构存储
        Document doc = documentBuilder.parse(inputStream);
        List<Student> studentList = new ArrayList<>();
        Student student;
        //4.从内存中读取数据
        //获取节点名称为student的所有节点
        NodeList students = doc.getElementsByTagName("student");
        for (int i = 0; i < students.getLength(); i++) {
            Node studentNode = students.item(i);
            student = new Student();
            //获取节点的属性值
            String id = studentNode.getAttributes().getNamedItem("id").getNodeValue();
            student.setId(id);
            //获取当前节点所有的子节点
            NodeList childNodes = studentNode.getChildNodes();
            for (int j = 0; j < childNodes.getLength(); j++) {
                Node item = childNodes.item(j);
                if ("name".equals(item.getNodeName())) {
                    //name节点的子节点
                    student.setName(item.getFirstChild().getNodeValue());
                } else if ("age".equals(item.getNodeName())) {
                    int age = Integer.valueOf(item.getFirstChild().getNodeValue());
                    student.setAge(age);
                }

            }
            studentList.add(student);
        }
        System.out.println(Arrays.toString(studentList.toArray()));
    }

}
